import { message } from "antd";
import { getPushDraftCheckList, getContentDetailsOther, getDetailesStatisticalData, pushDrafcheck, getPushedDraftList, getPushedDraftData, pushSiteList ,pushLibraryStats} from "../services/services";
export default {
  namespace: "push",

  state: {
    tableData: [],
    total: 0,
    detail: {},
    statistiData: [],
    pushedData: [],
    pushedTotal: 0,
    pushedTableData: [],
    pushedTableLoading: false,
    pushStatsData:[]
  },
  // 订阅
  subscriptions: {},
  // 同步
  reducers: {
    setTableData(state, action) {
      return { ...state, tableData: action.tableData };
    },
    setTotal(state, action) {
      return { ...state, total: action.total };
    },
    setDetail(state, action) {
      return { ...state, detail: action.detail };
    },
    setStatistiData(state, action) {
      return { ...state, statistiData: action.statistiData };
    },
    setPushedData(state, action) {
      return { ...state, pushedData: action.pushedData };
    },
    setPushedTotal(state, action) {
      return { ...state, pushedTotal: action.pushedTotal };
    },
    setPushedTableData(state, action) {
      return { ...state, pushedTableData: action.pushedTableData };
    },
    setPushedTableLoading(state, action) {
      return { ...state, pushedTableLoading: action.pushedTableLoading };
    },
    setPushSiteList(state, action) {
      return { ...state, pushSiteList: action.pushSiteList };
    },
    setpushStatsData(state,action){
      return {...state,pushStatsData:action.pushStatsData}
    }
  },
  // 异步
  effects: {
    /**
     * 获取推稿待审核列表
     */
    *getPushDraftCheckList({ payload }, { call, put, select }) {
      const res = yield call(getPushDraftCheckList, payload);
      if (res.code === 200) {
        yield put({ type: "setTableData", tableData: res.data.data });
        yield put({ type: "setTotal", total: res.data.total });
        return true;
      } else {
        message.error(res.msg);
        return false;
      }
    },
    /**
     * 获取推稿待审核列表-文章详情
     */
    *getContentDetailsOther({ payload }, { call, put, select }) {
      const res = yield call(getContentDetailsOther, payload);
      if (res.code === 200) {
        yield put({ type: "setDetail", detail: res.data });
        return true;
      } else {
        message.error(res.msg);
        return false;
      }
    },
    /**
     * 获取推稿待审核列表-文章详情统计数据
     */
    *getDetailesStatisticalData({ payload }, { call, put, select }) {
      const res = yield call(getDetailesStatisticalData, payload);
      if (res.code === 200) {
        let sdata = [];
        let arr = ["阅读量", "评论量", "点赞量", "分享量"];
        let arr1 = ["scan_count", "comment_count", "like_count", "share_count"];
        for (let i = 0; i < 4; i++) {
          let obj = {};
          obj["x"] = arr[i];
          obj["y"] = res.data[arr1[i]];
          sdata.push(obj);
        }
        yield put({ type: "setStatistiData", statistiData: sdata });
        return true;
      } else {
        message.error(res.msg);
        return false;
      }
    },
    /**
     * 通过审核
     */
    *pushDrafcheck({ payload }, { call, put, select }) {
      const res = yield call(pushDrafcheck, payload);
      if (res.code === 200) {
        message.success(res.msg);
        return true;
      } else {
        message.error(res.msg);
        return false;
      }
    },
    /**
     * 已推送列表
     */
    *getPushedDraftList({ payload }, { call, put, select }) {
      const res = yield call(getPushedDraftList, payload);
      if (res.code === 200) {
        yield put({ type: "setPushedData", pushedData: res.data.data });
        yield put({ type: "setPushedTotal", pushedTotal: res.data.total });
        return true;
      } else {
        message.error(res.msg);
        return false;
      }
    },
    /**
     * 已推送列表-详情表格
     */
    *getPushedDraftData({ payload }, { call, put, select }) {
      yield put({ type: "setPushedTableLoading", pushedTableLoading: true });
      const res = yield call(getPushedDraftData, payload);
      if (res.code === 200) {
        yield put({ type: "setPushedTableData", pushedTableData: res.data.data });
        yield put({ type: "setPushedTableLoading", pushedTableLoading: false });
        return true;
      } else {
        message.error(res.msg);
        return false;
      }
    },
    /**
     * 供稿资源库-推稿统计
     */
     *getpushLibraryStats({ payload }, { call, put, select }) {
      yield put({ type: "setPushedTableLoading", pushedTableLoading: true });
      const res = yield call(pushLibraryStats, payload);
      if (res.code === 200) {
        yield put({ type: "setpushStatsData", pushStatsData: res.data.data });
        yield put({ type: "setPushedTableLoading", pushedTableLoading: false });
        return true;
      } else {
        message.error(res.msg);
        return false;
      }
    }
  }
};
